# Register your models here.
from django.contrib import admin
from django.core.cache import cache
from goods.models import GoodsType

from goods.models import IndexPromotionBanner,IndexTypeGoodsBanner,GoodsSKU

from goods.models import IndexGoodsBanner
from celery_tasks.tasks import generate_static_index_html


class BaseModelAdmin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):
        """新增更新表中的数据时调用"""
        super().save_model(request, obj, form, change)

        # 发出任务，让celery worker重新生成新的index静态页面
        # generate_static_index_html.delay()
        # 清除首页缓存数据
        cache.delete("index_page_data")

    def delete_model(self, request, obj):
        """删除表中的数据时调用"""
        generate_static_index_html.delay()
        # 清除首页缓存数据
        cache.delete("index_page_data")


class GoodsTypesAdmin(BaseModelAdmin):
    pass


class IndexGoodsBannerAdmin(BaseModelAdmin):
    pass


class IndexTypeGoodsBannerAdmin(BaseModelAdmin):
    pass


class IndexPromotionBannerAdmin(BaseModelAdmin):
    pass


admin.site.register(GoodsType,GoodsTypesAdmin)
admin.site.register(IndexGoodsBanner, IndexGoodsBannerAdmin)
admin.site.register(IndexTypeGoodsBanner, IndexTypeGoodsBannerAdmin)
admin.site.register(IndexPromotionBanner, IndexPromotionBannerAdmin)
admin.site.register(GoodsSKU)
